package com.serviceprovider.business.module.admin.license.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.license.model.License;
import com.serviceprovider.business.module.admin.license.model.LicenseForm;
import com.serviceprovider.business.module.admin.license.model.LicenseRoleApplyForm;
import com.serviceprovider.business.module.admin.license.services.dao.interfaces.ILicenseRoleApplyService;
import com.serviceprovider.business.module.admin.license.services.dao.interfaces.ILicenseService;
import com.serviceprovider.business.module.admin.role.services.dao.interfaces.IRoleService;
import com.serviceprovider.business.utility.AppUtil;
import com.serviceprovider.business.utility.ApplicationConfigProperties;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LicenseController {
    @Autowired
    ApplicationConfigProperties applicationConfigProperties;
    @Autowired
    ILicenseRoleApplyService    licenseRoleApplyService;
    @Autowired
    ILicenseService             licenseService;
    @Autowired
    IRoleService                roleService;

    @RequestMapping(value = RequestConstants.LicenseRequests.INDEX_LICENSE)
    public ModelAndView enterLicense() {
        return new ModelAndView(RequestConstants.LicenseRequests.VIEW_INDEX_LICENSE);
    }

    @RequestMapping(
        value  = RequestConstants.LicenseRequests.CREATE_LICENSE,
        method = RequestMethod.GET
    )
    public ModelAndView enterCreateLicense() {
        ModelAndView mav = new ModelAndView(RequestConstants.LicenseRequests.VIEW_CREATE_LICENSE);

        mav.addObject("licenseForm", new LicenseForm());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.LicenseRequests.CREATE_LICENSE,
        method = RequestMethod.POST
    )
    public ModelAndView createLicense(@ModelAttribute("licenseForm") LicenseForm form) throws IOException {
        ModelAndView mav     = new ModelAndView("redirect:" + RequestConstants.LicenseRequests.VIEW_LICENSES);
        License      license = form.getLicense();

        form.applyUploadedData();
        this.licenseService.insertLicense(license);
        mav.addObject("licenseForm", new LicenseForm(license));

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.LicenseRequests.VIEW_LICENSES,
        method = RequestMethod.GET
    )
    public ModelAndView viewLicenses(HttpServletRequest request) {
        ModelAndView mav        = new ModelAndView(RequestConstants.LicenseRequests.VIEW_VIEW_LICENSES);
        int          recordSize = applicationConfigProperties.getRecordSize("view_licenses.records.size");
        int          pageNo     = 1;

        try {
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        } catch (NumberFormatException ex) {}

        String sortOrder = ((request.getParameter("sortOrder") == null)
                            || (request.getParameter("sortOrder").length() == 0))
                           ? "asc"
                           : request.getParameter("sortOrder");
        String sortColumn = ((request.getParameter("sortColumn") == null)
                             || (request.getParameter("sortColumn").length() == 0))
                            ? "id"
                            : request.getParameter("sortColumn");

        mav.addObject("licenseList", licenseService.findAll(pageNo, recordSize, sortColumn, sortOrder));

        // mav.addObject("roleList", roleService.findAll());
        System.out.println("viewLicenses() method");

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.LicenseRequests.LICENSE_APPLICATIONS,
        method = RequestMethod.GET
    )
    public ModelAndView viewLicenseApplications() {
        ModelAndView mav = new ModelAndView(RequestConstants.LicenseRequests.VIEW_LICENSE_APPLICATIONS);

        mav.addObject("licenseList", licenseService.findAll());
        mav.addObject("roleList", roleService.findAll());
        mav.addObject("licenseRoleApplyList", licenseRoleApplyService.findAll());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.LicenseRequests.LICENSE_APPLICATIONS,
        method = RequestMethod.POST
    )
    public ModelAndView saveLicenseApplications(@ModelAttribute("licenseRoleApplyForm") LicenseRoleApplyForm form) {
        ModelAndView mav = new ModelAndView(RequestConstants.LicenseRequests.VIEW_LICENSE_APPLICATIONS);

        // if(true){throw new RuntimeException("x = "+form.getLicenseRoleApplyList().size());}
        licenseRoleApplyService.save(form.getLicenseRoleApplyList());
        mav.addObject("licenseList", licenseService.findAll());
        mav.addObject("roleList", roleService.findAll());
        mav.addObject("licenseRoleApplyList", licenseRoleApplyService.findAll());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.LicenseRequests.EDIT_LICENSE,
        method = RequestMethod.GET
    )
    public ModelAndView enterEditLicense(HttpServletRequest req) {
        ModelAndView mav = new ModelAndView(RequestConstants.LicenseRequests.VIEW_EDIT_LICENSE);
        int          id  = Integer.parseInt(req.getParameter("id"));

        mav.addObject("licenseForm", new LicenseForm(licenseService.getLicenseById(id)));

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.LicenseRequests.EDIT_LICENSE,
        method = RequestMethod.POST
    )
    public ModelAndView editLicense(@ModelAttribute("licenseForm") LicenseForm form) throws IOException {
        ModelAndView mav     = new ModelAndView("redirect:" + RequestConstants.LicenseRequests.VIEW_LICENSES);
        License      license = form.getLicense();

        form.applyUploadedData();
        licenseService.updateLicense(license);
        mav.addObject("licenseForm", new LicenseForm(license));

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.LicenseRequests.DELETE_LICENSE,
        method = RequestMethod.POST
    )
    public ModelAndView deleteRoles(HttpServletRequest req) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.LicenseRequests.VIEW_LICENSES);

        licenseService.deleteLicensesById(AppUtil.toInt(req.getParameterValues("ids")));

        return mav;
    }

    @RequestMapping(value = RequestConstants.LicenseRequests.DOWNLOAD_LICENSE)
    public ModelAndView downloadLicense(HttpServletRequest req) {
        ModelAndView mav = new ModelAndView(RequestConstants.LicenseRequests.VIEW_DOWNLOAD_LICENSE);
        int          id  = Integer.parseInt(req.getParameter("id"));

        mav.addObject("license", licenseService.getLicenseById(id));

        return mav;
    }
}



